//
//  AccountTicketCell.m
//  ImGuider
//
//  Created by llt on 2017/6/21.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountTicketCell.h"

@interface AccountTicketCell()
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet YYLabel *describeLabel;

@property (weak, nonatomic) IBOutlet UIImageView *shareTagImageView;
@property (weak, nonatomic) IBOutlet UILabel *shareTagLabel;
@end

@implementation AccountTicketCell


- (void)awakeFromNib {
    
    [super awakeFromNib];
    
//    self.useBtn.layer.borderWidth = 1.0f;
//    self.useBtn.layer.borderColor = kThemeColor.CGColor;
//    self.useBtn.titleLabel.text = MYLocalizedString(@"VOUCHER_USE_NOW", nil);
    
    self.describeLabel.numberOfLines = 0;
    self.describeLabel.font = Font(12);
    self.describeLabel.textColor = kContentColor;
}


- (void)setCoupon:(Coupon *)coupon {
    
    _coupon = coupon;
    self.nameLabel.text = coupon.infos[@"title"];
    self.typeLabel.text = coupon.infos[@"middle"];
    self.priceLabel.text = coupon.infos[@"big"];
    
    
    if (self.pageType == 1) {
       
        self.describeLabel.text = self.coupon.infos[@"desc"];
        self.shareTagLabel.hidden = YES;
        self.shareTagImageView.hidden = YES;
        
    } else {
        
        self.describeLabel.attributedText = [self getYYLabelAttributedText];
        
        if (coupon.cangive.boolValue) {
            self.shareTagLabel.hidden = NO;
            self.shareTagLabel.text = MYLocalizedString(@"TICKET_COUPON_CANSEND", nil);
            self.shareTagImageView.hidden = NO;
        } else {
            self.shareTagLabel.hidden = YES;
            self.shareTagImageView.hidden = YES;
        }
    }
}

/** 获取富文本 */
- (NSAttributedString *)getYYLabelAttributedText {
    
    NSString *textString = self.coupon.infos[@"desc"];
    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
    mutableAttributedString.font = Font(12);
    mutableAttributedString.color = kContentColor;
    mutableAttributedString.lineSpacing = 8;
    
    if (![self.coupon.infos[@"canquickuse"] boolValue]) {

        return mutableAttributedString;
    }
    
    NSString *highlightString = [NSString stringWithFormat:@"\t%@>>",MYLocalizedString(@"VOUCHER_USE_NOW", nil)];
    
    [mutableAttributedString appendString:highlightString];
    
    NSRange range1 = NSMakeRange(textString.length, highlightString.length);
    
    [mutableAttributedString setColor:kThemeColor range:range1];
    
    YYTextHighlight *stepHighlight = [YYTextHighlight highlightWithBackgroundColor:kBGColor];
    [mutableAttributedString setTextHighlight:stepHighlight range:range1];
    
    stepHighlight.tapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        if ([_delegate respondsToSelector:@selector(useCouponCell:coupon:)]) {
            [_delegate useCouponCell:self coupon:self.coupon];
        }
    };
    
    return mutableAttributedString;
}

- (IBAction)useCouponAction:(id)sender {
    
    if ([_delegate respondsToSelector:@selector(useCouponCell:coupon:)]) {
        [_delegate useCouponCell:self coupon:self.coupon];
    }
}

@end
